package smartclass.shape;

import smartclass.data.FinalValues;
import smartclass.present.StudentActivity;
import smartclass.slidingmenu.example.LeftAndRightActivity;
import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.view.View;

import com.samsung.android.sdk.pen.engine.SpenSurfaceView;

@SuppressLint("ViewConstructor")
public class ReviewDrawStudent extends View {
	public Paint paint;
	private float r;
	public Canvas mCanvas;
	private SpenSurfaceView mSpenSurfaceView;
	private StudentActivity context;

	public ReviewDrawStudent(StudentActivity context,
			SpenSurfaceView mSpenSurfaceView) {
		super(context);
		this.context = context;
		// TODO Auto-generated constructor stub
		this.mSpenSurfaceView = mSpenSurfaceView;
		// mBitmap = Bitmap.createBitmap(src)
		paint = new Paint(Paint.DITHER_FLAG);
		paint.setDither(true);
		paint.setColor(Color.GREEN);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeWidth(2);
		paint.setPathEffect(new DashPathEffect(new float[] { 10, 10 }, 150));
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		// mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		// mCanvas = new Canvas(mBitmap);

	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		PointF startPointReal = new PointF(0, 0), currentPointReal = new PointF(
				0, 0);
		float ratio = mSpenSurfaceView.getZoomRatio();
		paint.setStrokeWidth(2 * ratio);
		for (int i = 0; i < 2; i = i + 2) {
			if (ratio < 1) {
				startPointReal.set(
						(context.startPoint.x)
								+ mSpenSurfaceView.getFrameStartPosition().x,
						(context.startPoint.y)
								+ mSpenSurfaceView.getFrameStartPosition().y);
				currentPointReal.set(
						(context.currentPoint.x)
								+ mSpenSurfaceView.getFrameStartPosition().x,
						(context.currentPoint.y)
								+ mSpenSurfaceView.getFrameStartPosition().y);
			} else {
				startPointReal = context.startPoint;
				currentPointReal = context.currentPoint;
			}
		}
		mCanvas = canvas;
		if (context.getMode() == 6) {
			switch (context.getModeBasicShape()) {
			case FinalValues.LINE:
				drawLine(startPointReal, currentPointReal);
				break;
			case FinalValues.RECTANGLE:
				drawRect(startPointReal, currentPointReal);
				break;
			case FinalValues.TRIANGLE:
				drawTriagle(startPointReal, currentPointReal);
				break;
			case FinalValues.RIGHTSTRIANGLE:
				drawRightTriangle(startPointReal, currentPointReal);
				break;
			case FinalValues.CIRCLE:
				drawCircle(startPointReal, currentPointReal);
				break;
			case FinalValues.ELIP:
				drawElip(startPointReal, currentPointReal);
				break;
			case FinalValues.PARABOL:
				drawParabol(startPointReal, currentPointReal);
				break;
			case FinalValues.PARALLEL:
				drawParallelogram(startPointReal, currentPointReal);
				break;
			case FinalValues.RHOMBUS:
				drawRhombus(startPointReal, currentPointReal);
				break;
			case FinalValues.ISOSCELES_TRAPEZOID:
				drawIsoscelesTrapezoid(startPointReal, currentPointReal);
				break;
			case FinalValues.ISOSCELES_SQUARE:
				drawIsoscelesSquare(startPointReal, currentPointReal);
				break;
			case FinalValues.SYSTEM_DIMENSION:
				drawLine(new PointF(currentPointReal.x, 0), new PointF(
						currentPointReal.x, mCanvas.getHeight()));
				drawLine(new PointF(0, currentPointReal.y),
						new PointF(mCanvas.getWidth(), currentPointReal.y));
				break;
			default:
				break;
			}
		}

		invalidate();
	}

	public void drawLine(PointF p1, PointF p2) {
		mCanvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint);
	}

	public void drawRect(PointF p1, PointF p2) {
		if (p1.x < p2.x && p1.y < p2.y) {
			mCanvas.drawRect(p1.x, p1.y, p2.x, p2.y, paint);
		}
		if (p1.x > p2.x && p1.y < p2.y) {
			mCanvas.drawRect(p2.x, p1.y, p1.x, p2.y, paint);
		}
		if (p1.x < p2.x && p1.y > p2.y) {
			mCanvas.drawRect(p1.x, p2.y, p2.x, p1.y, paint);
		}
		if (p1.x > p2.x && p1.y > p2.y) {
			mCanvas.drawRect(p2.x, p2.y, p1.x, p1.y, paint);
		}
	}

	public void drawTriagle(PointF p1, PointF p2) {
		mCanvas.drawLines(new float[] { p1.x, p1.y, p2.x, p2.y, p2.x, p2.y,
				2 * p1.x - p2.x, p2.y, 2 * p1.x - p2.x, p2.y, p1.x, p1.y },
				paint);
	}

	public void drawRightTriangle(PointF p1, PointF p2) {
		mCanvas.drawLines(new float[] { p1.x, p1.y, p2.x, p2.y, p2.x, p2.y,
				p1.x, p2.y, p1.x, p2.y, p1.x, p1.y }, paint);
	}

	public void drawCircle(PointF p1, PointF p2) {

		r = (float) Math.sqrt((Double) (Math.pow(p1.x - p2.x, 2) + Math.pow(
				p1.y - p2.y, 2)));
		mCanvas.drawCircle(p1.x, p1.y, r, paint);
	}

	public void drawElip(PointF p1, PointF p2) {
		RectF rect = new RectF(2 * p1.x - p2.x, 2 * p1.y - p2.y, p2.x, p2.y);
		mCanvas.drawOval(rect, paint);
	}

	public void drawParabol(PointF p1, PointF p2) {
		int pointSize = (int) (2 * (Math.abs(p1.x - p2.x)));
		PointF[] points = new PointF[pointSize];
		for (int i = 0; i < pointSize; i++) {
			points[i] = new PointF();
			points[i].x = i - pointSize / 2 + p1.x;
			points[i].y = (float) ((p2.y - p1.y) * Math.pow((i - pointSize / 2)
					/ (p2.x - p1.x), 2))
					+ p1.y;
			mCanvas.drawPoint(points[i].x, points[i].y, paint);
		}

	}

	public void drawParallelogram(PointF p1, PointF p2) {
		mCanvas.drawLines(new float[] { p1.x, p1.y, (2 * p1.x + p2.x) / 3,
				p2.y, (2 * p1.x + p2.x) / 3, p2.y, p2.x, p2.y, p2.x, p2.y,
				(2 * p2.x + p1.x) / 3, p1.y, (2 * p2.x + p1.x) / 3, p1.y, p1.x,
				p1.y }, paint);
	}

	public void drawRhombus(PointF p1, PointF p2) {
		mCanvas.drawLines(new float[] { p1.x, p1.y, 2 * p1.x - p2.x, p2.y,
				2 * p1.x - p2.x, p2.y, p1.x, 2 * p2.y - p1.y, p1.x,
				2 * p2.y - p1.y, p2.x, p2.y, p2.x, p2.y, p1.x, p1.y }, paint);
	}

	public void drawIsoscelesTrapezoid(PointF p1, PointF p2) {
		mCanvas.drawLines(new float[] { p1.x, p1.y, (4 * p1.x - p2.x) / 3,
				p2.y, (4 * p1.x - p2.x) / 3, p2.y, p2.x, p2.y, p2.x, p2.y,
				(p1.x + 2 * p2.x) / 3, p1.y, (p1.x + 2 * p2.x) / 3, p1.y, p1.x,
				p1.y }, paint);
	}

	public void drawIsoscelesSquare(PointF p1, PointF p2) {
		mCanvas.drawLines(new float[] { p1.x, p1.y, p1.x, p2.y, p1.x, p2.y,
				p2.x, p2.y, p2.x, p2.y, (p1.x + p2.x) / 2, p1.y,
				(p1.x + p2.x) / 2, p1.y, p1.x, p1.y }, paint);
	}
}
